<template>
  <div class="index">


    <!--      工具条-->
    <div style="margin: 20px;">
      <el-button type="danger" size="small" icon="el-icon-refresh" @click="reload"> 刷 新</el-button>
    </div>

    <!--      表格-->
    <div class="table">
      <el-table
        :data="roleList"
        element-loading-text="Loading"
        border
        fit
        :stripe="true"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column
          align="center"
          prop="roleId"
          label="ID"
          sortable
          width="180">
        </el-table-column>
        <el-table-column
          align="center"
          prop="name"
          label="角色名称"
          sortable
          width="250">
        </el-table-column>
        <el-table-column
          align="center"
          prop="description"
          label="角色描述">
        </el-table-column>
        <el-table-column
          align="center"
          prop="permissionList.length"
          label="权限数量">
        </el-table-column>
        <el-table-column
          align="center"
          prop="createTime"
          label="创建时间">
        </el-table-column>
        <el-table-column
          align="left">
          <template slot="header" slot-scope="scope" label="操作">
          </template>
          <template slot-scope="scope">
            <el-button

              size="small"
              type="success"
              icon="el-icon-edit"
              @click="openAuthorizeDialog(scope.$index, scope.row)"
            >授 权
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>


    <!--    删除对话框-->
    <div class="dialog">
      <el-dialog title="角色授权" :visible.sync="authorizeDialog" width="40%" destroy-on-close="true"
                 :close-on-click-modal='false'>

        <el-transfer
          filterable
          :titles="['未授权权限','已授权权限']"
          :button-texts="['减少权限', '添加权限']"
          :props="{key:'permissionId',label:'description'}"
          filter-placeholder="请输入权限名称"
          v-model="AuthorizePermission"
          :right-default-checked="AuthorizePermission"
          :data="permissionList">
        </el-transfer>

        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="authorizeDialog = false">取 消</el-button>
          <el-button size="small"
                     type="success"
                     icon="el-icon-edit" @click="updateRolePermission">更新角色权限
          </el-button>
        </div>
      </el-dialog>
    </div>


  </div>

</template>

<script>
  import { getRoleList } from '@/api/role'
  import { getPermissionList } from '@/api/permission'
  import { updateRolePermission } from '@/api/role_permission'
  import permission from '../../store/modules/permission'

  export default {
    name: 'index',
    inject: ['reload'],
    data() {
      return {
        // 标签列表
        roleList: [],
        permissionList: [],
        roleInfo: {
          roleId: null,
          name: '',
          description: '',
          status: null,
          createTime: null,
          permissionList: []
        },

        //已授权数组
        AuthorizePermission: [],
        authorizeDialog: false //授权对话框

      }
    },
    mounted: function() {
      /**
       * 获取标签列表
       */
      this.getRoleList()
    },
    methods: {
      /**
       * 获取用户列表
       */
      getRoleList() {

        // api获取
        getRoleList().then(response => {

          if (response.code == 200) {
            const { data } = response
            // 更新数据
            this.roleList = data //数据

          }

        }).catch(error => {
          console.log(error)
        })

      },

      /**
       * 更新用户权限
       * */
      updateRolePermission() {

        // api获取
        updateRolePermission(this.roleInfo.roleId, JSON.stringify(this.AuthorizePermission)).then(response => {

          if (response.code == 200) {

            this.authorizeDialog = false
            this.reload()

          }

        }).catch(error => {
          console.log(error)
        })

      },

      /**
       * 打开删除用户对话框
       * */
      openAuthorizeDialog(index, row) {
        this.roleInfo = JSON.parse(JSON.stringify(row))
        this.AuthorizePermission = []

        // 权限列表为空 获取权限
        if (this.permissionList.length == 0) {
          // api获取
          getPermissionList().then(response => {

            if (response.code == 200) {
              const { data } = response
              this.permissionList = data
              this.authorizeDialog = true  //打开对话框
              // 获取授权数组
              this.getAuthorizePermission()
            }

          }).catch(error => {
            console.log(error)
          })
        } else {
          this.authorizeDialog = true  //打开对话框
        }

        // 获取授权数组
        this.getAuthorizePermission()

      },

      /***
       * 获取授权数组
       */
      getAuthorizePermission() {

        this.permissionList.forEach(permission => {
          this.roleInfo.permissionList.forEach(item => {
            if (item.permissionId == permission.permissionId) {
              console.log(JSON.stringify(item))
              this.AuthorizePermission.push(item.permissionId)
            }
          })
        })

      }

    },
    filters: {
      formatSex: function(value) {
        console.log('性别：' + value)
        if (value == 0) return '男'
        if (value == 1) return '女'
      }
    }
  }
</script>

<style scoped>
  .index {
    margin: 10px 20px;
  }

  .index-table-expand {
    font-size: 0;
  }

  .index-table-expand label {
    width: 90px;
    color: #99a9bf;
  }

  .index-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
